package com.zhangzhan.gulimallcart.web;

import com.zhangzhan.gulimallcart.service.CartService;
import com.zhangzhan.gulimallcart.vos.CartItemVo;
import com.zhangzhan.gulimallcart.vos.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/6/28 23:58
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @RequestMapping(value = "/cartList.html", method = RequestMethod.GET)
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        CartVo cartVo = cartService.cartList();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    @RequestMapping(value = "/addCart", method = RequestMethod.GET)
    public String addCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, Model model, RedirectAttributes attributes) throws ExecutionException, InterruptedException {

        CartItemVo cartItemVo = cartService.addCart(skuId, num,false,null);

        //作用：重定向时可以以路径后缀拼在后面作为参数，只能一次
        attributes.addAttribute("skuId", cartItemVo.getSkuId());
        return "redirect:http://cart.gulimall.com/getCacheCart";
    }


    /**
     * @param skuId
     * @param model
     * @return java.lang.String
     * @description 防止页面重新提交
     * @author zhangyiheng
     * @date 2024/6/29 11:08
     */
    @RequestMapping(value = "/getCacheCart", method = RequestMethod.GET)
    public String getCacheCart(@RequestParam("skuId") Long skuId,
                               Model model) throws ExecutionException, InterruptedException {

        CartItemVo cartItemVo = cartService.getCacheCart(skuId);

        model.addAttribute("item", cartItemVo);

        return "index";
    }


    /**
     * 修改选中字段
     * @param skuId
     * @param checkedNum
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping(value = "/selected", method = RequestMethod.GET)
    public String selected(@RequestParam("skuId") Long skuId,
                          @RequestParam("checkedNum") Integer checkedNum) {
        cartService.checked(skuId,checkedNum);

        return "redirect:http://cart.gulimall.com/cartList.html";
    }

    /**
     * @description 修改数量
     * @param skuId
     * @param count
     * @return java.lang.String
     * @author zhangyiheng
     * @date 2024/6/29 13:43
     */
    @RequestMapping(value = "/onOutNum", method = RequestMethod.GET)
    public String onOutNum(@RequestParam("skuId") Long skuId,
                           @RequestParam("count") Integer count) {
        cartService.onOutNum(skuId,count);

        return "redirect:http://cart.gulimall.com/cartList.html";
    }

    /**
     * @description 删除
     * @param skuId
     * @param
     * @return java.lang.String
     * @author zhangyiheng
     * @date 2024/6/29 13:43
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam("skuId") Long skuId)  {
        cartService.delete(skuId);

        return "redirect:http://cart.gulimall.com/cartList.html";
    }



    @ResponseBody
    @RequestMapping(value = "/getMemberCarts", method = RequestMethod.GET)
    public List<CartItemVo> getMemberCarts()  {

        return cartService.getMemberCarts();
    }
}
